<?php

require_once("View/View/LoggedInView.php");
require_once("Database/Model/ExerciseDAL.php");
require_once("Database/Model/MyWorkoutDAL.php");
require_once("Exercises/View/ExercisesView.php");
require_once("MyWorkouts/Model/WorkoutModel.php");
require_once("Database/Model/WorkoutExercisesDAL.php");

class MyWorkoutsView
{
	/**
	*@var ExerciseDAL 
	**/
	private $exerciseDAL;
	/**
	*@var LoggedInView 
	**/
	private $loggedInView;
	/**
	*@var MyWorkoutDAL 
	**/
	private $myWorkoutDAL;
	/**
	*@var ExerciseView        
	**/
	private $exerciseView;
	/**
	*@var WorkoutModel 
	**/      
	private $myWorkoutModel;
	/**
	*@var WorkoutExercisesDAL 
	**/
	private $workoutExercisesDAL;

	/**
	*@var string post 
	**/
	private static $newWorkoutName = "MyWorkoutsView::newWorkoutName";
	/**
	*@var string post 
	**/
	private static $newWorkoutDescription = "MyWorkoutsView::newWorkoutDescription";

	/**
	*@var string post 
	**/
	private static $editWorkoutName = "MyWorkoutsView::editWorkoutName";
	/**
	*@var string post 
	**/
	private static $editWorkoutDescription = "MyWorkoutsView::editWorkoutDescription";
	
	/**
	*@var string session
	**/
	private static $allExercises = "MyWorkoutsView::GetAllExercises";

	/**
	*@var string post 
	**/
	private static $exerciseOrder ="MyWorkoutView::exerciseOrder"; 
	/**
	*@var string post 
	**/
	private static $exerciseReps = "MyWorkoutView::exerciseReps"; 
	/**
	*@var string post 
	**/
	private static $exerciseSets = "MyWorkoutView::exerciseSets";

	/**
	*@var string array get 
	**/
	private static $allAvailableWorkouts = "allAvailableWorkouts";

	/**
	*@var string get 
	**/
	private static $add = "add";
	/**
	*@var string get 
	**/
	private static $added = "added";

	/**
	*@var string get 
	**/
	private static $edit = "edit";
	/**
	*@var string get 
	**/
	private static $edited = "edited";

	/**
	*@var string get 
	**/
	private static $idToRead = "read";
	/**
	*@var string get 
	**/
	private static $remove = "remove";

	/**
	*@var string css id 
	**/
	private static $nameTextField = "NameTextField"; 
	/**
	*@var string css id 
	**/
	private static $descriptionTextBox = "DescriptionTextBox";
	/**
	*@var string css id 
	**/
	private static $formSubmitButton = "FormSubmitButton";

	public function __construct()
	{	 
		$this->myWorkoutModel = new WorkoutModel();
		$this->exerciseDAL = new ExerciseDAL();
		$this->loggedInView = new LoggedInView();
		$this->myWorkoutDAL = new MyWorkoutDAL();
		$this->exerciseView = new ExersicesView();
		$this->workoutExercisesDAL = new workoutExercisesDAL();
	}

	/**
	*@return string html 
	**/
	private function getMessage()
	{	
		if($this->myWorkoutModel->getErrorMessage() != null)
		{
			return "<div id='ErrorMessageDiv'><p>" . $this->myWorkoutModel->getErrorMessage() . "</p></div>";
		}
		elseif($this->myWorkoutModel->getSuccesMessage() != null)
		{
			return "<div id='SuccessMessageDiv'><p>" . $this->myWorkoutModel->getSuccesMessage() . "</p></div>";
		}
		return "";
	}

	/**
	*@return string html 
	**/
	public function getMyWorkoutsPage()
	{
		$html = $this->getMyWorkoutsPageHeader();
		$html .= $this->getMyWorkoutsPageBody();
		$html .= $this->getMyWorkoutsPageFooter();
		return $html;
	}

	/**
	*@return string html 
	**/
	private function getMyWorkoutsPageHeader()
	{
		$header = $this->loggedInView->getLoggedInHeader();
		return $header;
	}

	/**
	*@return string html 
	**/
	private function getMyWorkoutsPageBody()
	{
		$html = $this->getMenu();                   
		$html .= $this->getMessage();
	  	if($this->isItAdd() || $this->isItAdded())
	  	{
	  		$html .= $this->createNewWorkoutForm();
	  	}
	  	elseif($this->isItEdit() || $this->isItEdited())
	  	{
	  		$html .= $this->getEditForm();
	  	}
	  	elseif($this->ifReadWorkout())
	  	{
	  		$workoutID = $this->getIdToRead();
	  		$html .= $this->getAllWorkoutsHtml($workoutID);
	  	}
	  	else
	  	{
	  		$html .= "<div id='myWorkoutsContainer'>
				  	    <ul>
				  	    	<li>
								<a href='MyWorkouts.php?".self::$add."'>Skapa nytt träningsupplägg</a>
							</li>
						</ul>
				  </div>";
	  		$html .= $this->getAllWorkoutsHtml();
	  	}
	  
		return $html;

	}

	/**
	*@param int workoutID
	*@return string html 
	**/
	private function getAllWorkoutsHtml($workoutID = null)
	{
		$html = "<div id='WorkoutsDiv'>";
		$workouts;
		$exerciseHTML ="";
		
		$workouts = $this->myWorkoutDAL->getWorkouts($workoutID); 
		if($workoutID != null && $workouts == null)
		{
			$html .= "<p> Det här träningsschemat finns ej, eller är så har du ej behörighet att se det </p>";
		}
		elseif($workouts != null && $workoutID == null)
		{
			foreach($workouts as $wo)
			{
				$html .= $this->createWorkoutHTML($wo);
			}
		}
		elseif($workouts != null && $workoutID != null)
		{
			$html .= $this->createWorkoutHTML($workouts);
			$exerciseHTML = $this->getAllExercisesInOneWorkout($workoutID);
		}
		elseif($workouts == null && $workoutID == null)
		{
			$html .= "<p> Du har ej skapat några träningsscheman ännu</p>";
		}
		
		$html .= "</div>";
		$html .= $exerciseHTML;
		return $html;
	}

	/**
	*@param int 
	*@return string html
	**/
	private function createWorkoutHTML($workout)
	{
		$html .= "<div id='OneWorkoutDiv'><a href='?read=".$workout->getID()."'>Visa hela</a>";
		$html .= "<a href='?remove=".$workout->getID()."'> Ta bort</a>";
		$html .= "<h2>" . $workout->getName()        . "</h2>";
		$html .= "<p>"  . $workout->getDescription() . "</h2></div>";
		return $html;
	}


	/**
	*@param int
	*@return array() Exercises 
	**/
	public function getAllExercisesInOneWorkout($workoutID)
 	{
 		$exercises = $this->workoutExercisesDAL->getAllWorkoutExercisesList($workoutID);
 		$exer = $this->exerciseView->getExercises("", $exercises);
 		return $exer;
 	}


 	/**
	*@return bool 
	**/
 	private function ifReadWorkout()
 	{
 		if(isset($_GET[self::$idToRead]))
 		{
 			return true;
 		}
 		return false;
 	}

 	/**
	*@return int GET 
	**/
 	private function getIdToRead()
 	{
 		if(isset($_GET[self::$idToRead]) && !empty($_GET[self::$idToRead]))
 		{
 			return $_GET[self::$idToRead];
 		}
 		return 0;
 	}

 

	/**
	*@return string html
	**/
	private function createNewWorkoutForm()
	{
		$html = "
		<div id='createMyWorkoutDiv' >
			<a href='MyWorkouts.php'>tillbaka</a>
			<form action='?".self::$added."' method='post' enctype='multipart/form-data'>
				<fieldset> 
					<legend>Lägg till övningar</legend>";

		$exercise = $this->exerciseDAL->getExerciseList();

		$html .= "<div id='MyWorkoutExercisesDiv'>";
		$name = $this->getNewWorkoutName();
		$description = $this->getNewWorkoutDescription();
		foreach($exercise as $ex) 
		{					  
			$html .= $this->getChooseExercisesHTML($ex);
		}
		$html .= "</div>";
		$html .= "  <div id='workoutViewExerciseName'><h3>Namn :</h3>
					<input type='text' size='40' name='" . self::$newWorkoutName . "' id='" . self::$nameTextField . "' value='$name' />
				    </div>
					<h3>Beskrivning :</h3>
					<textarea rows='4' cols='50' name='" . self::$newWorkoutDescription . "' id='" . self::$descriptionTextBox . "' >$description</textarea>

					<input type='submit' name='' id='".self::$formSubmitButton."' value='Skapa' />
				</fieldset>
			</form>
		</div>";

		return $html;

	}

	/**
	*@param exercise
	*@return string html 
	**/
	private function getChooseExercisesHTML($ex)
	{
		$eName = $ex->getExerciseName();
		$eID = $ex->getExerciseID();

		$html = "<div id='createMyWorkoutExercise'>
			<h3 id='checkboxLabel'>$eName</h3>
			<input type='checkbox' id='checkbox' name='".self::$allAvailableWorkouts."[]' value='".$eID."'/>
			<label for='".self::$exerciseOrder."' >Träningsordning :</label>
			<input type='text' size='3' name='".self::$exerciseOrder. $eID."' id='".self::$exerciseOrder."' value='' />
			<label for='".self::$exerciseReps."' >Antal reps :</label>
			<input type='text' size='3' name='".self::$exerciseReps.$eID."' id='".self::$exerciseReps."' value='' />
			<label for='".self::$exerciseSets."' >Antal sets :</label>
			<input type='text' size='3' name='".self::$exerciseSets.$eID."' id='".self::$exerciseSets."' value='' /></div>";

		return $html;
	}

	/**
	*@return int post
	**/
	public function getExerciseOrder()
	{
		$order = array();
		$chosenExersices = $this->getChosenExercises();
		foreach($chosenExersices as $exID)
		{
			if(isset($_POST[self::$exerciseReps.$exID]))
			{
				$order[$exID] = intval($_POST[self::$exerciseOrder.$exID]);
			}
		}
		return $order;
	}

	/**
	*@return int post
	**/
	public function getExerciseReps()
	{
		$reps = array();
		$chosenExersices = $this->getChosenExercises();
		foreach($chosenExersices as $exID)
		{
			if(isset($_POST[self::$exerciseReps.$exID]))
			{
				$reps[$exID] = intval($_POST[self::$exerciseReps.$exID]);
			}
		}
		return $reps;
	}

	/**
	*@return int array
	**/
	public function getExerciseSets()
	{
		$sets = array();
		$chosenExersices = $this->getChosenExercises();
		foreach($chosenExersices as $exID)
		{
			if(isset($_POST[self::$exerciseSets. $exID]))
			{
				$sets[$exID] = intval($_POST[self::$exerciseSets.$exID]);
			}
		}
		return $sets;
	}


	/**
	*@return array  
	**/
	public function getChosenExercises()
	{
		if(!empty($_POST[self::$allAvailableWorkouts]))
		{	
			return $_POST[self::$allAvailableWorkouts];
		}
		return array();
	}


	/**
	*@return string
	**/
	public function getNewWorkoutDescription()
	{
		if(isset($_POST[self::$newWorkoutDescription]))
		{
			return $_POST[self::$newWorkoutDescription];
		}
	}

	/**
	*@return string
	**/
	public function getNewWorkoutName()
	{
		if(isset($_POST[self::$newWorkoutName]))
		{
			return $_POST[self::$newWorkoutName];
		}
	}
	


	/**
	*@return bool
	**/
	public function isItRemove()
	{
		if(isset($_GET[self::$remove]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return bool
	**/
	private function isItAdd()
	{
		if(isset($_GET[self::$add]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return bool 
	**/
	public function isItAdded()
	{
		if(isset($_GET[self::$added]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return bool 
	**/
	private function isItEdit()
	{
		if(isset($_GET[self::$edit]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return bool 
	**/
	public function isItEdited()
	{
		if(isset($_GET[self::$edited]))
		{
			return true;
		}
		return false;
	}



	/**
	*@return int
	**/
	public function getIdToRemove()
	{
		if(isset($_GET[self::$remove]) && $_GET[self::$remove] != null)
		{
			return $_GET[self::$remove];
		}
		return 0;
	}


	/**
	*@return string
	**/
	private function getMenu()
	{
		$html = $this->loggedInView->getLoggedInMenu();
	    return $html;
	}


	/**
	*@return string
	**/
	private function getMyWorkoutsPageFooter()
	{
		$footer = $this->loggedInView->getFooter();
		return $footer;
	}




}